//
//  LS_VisitorLoginView.swift
//  weibo
//
//  Created by 罗李苏 on 16/5/20.
//  Copyright © 2016年 Apple. All rights reserved.
//

import UIKit
import SnapKit
//声明协议
@objc protocol LS_VisitorLoginViewDelegate:NSObjectProtocol{
    
    //声明协议方法
    
    //用户登录的协议方法
    optional func userWillLogin()
    
    //用户注册的协议方法
    func userWiilRegister()

}
class LS_VisitorLoginView: UIView {
    
    //声明代理属性 使用弱引用
    weak var visitorDelegate:LS_VisitorLoginViewDelegate?
    //MARK: 通过外界调用设置访客视图的页面信息
    func upDataUI(title: String, imageName: String, isHome: Bool = false){
        
        if isHome {
            //如果是首页 做动画
            startAnimation()
        }else{
            //不是首页
            //大房子隐藏
            largeHouse.hidden = true
            //圈圈移动到最顶层
            bringSubviewToFront(circleIcon)
        }
        circleIcon.image = UIImage(named: imageName)
        tipLabel.text = title
    }
    
    //MARK:给首页的圈圈添加动画
    private func startAnimation(){
        
        let basic = CABasicAnimation(keyPath: "transform.rotation")
        //动画时间
        basic.duration = 15
        //重复次数
        basic.repeatCount = MAXFLOAT
        //动画效果
        basic.toValue = 2 * M_PI
        //当动画结束 或者 视图处于非活跃状态 动画效果不移除图层 意思就是一直有动画效果
        basic.removedOnCompletion = false

        //给图层添加动画效果
        circleIcon.layer.addAnimation(basic, forKey: nil)
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //设置UI界面
    private func setUI(){
    
        addSubview(circleIcon)
        addSubview(backIcon)
        addSubview(largeHouse)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)

        //自动布局 使用系统自动布局类设置约束
        //约束应该添加到父视图 如果使用手写添加约束 需要手动关闭 frame 布局
//    circleIcon.translatesAutoresizingMaskIntoConstraints = false
//       self.addConstraint(NSLayoutConstraint(item: circleIcon, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
//        
//        self.addConstraint(NSLayoutConstraint(item: circleIcon, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
        
        //使用第三方框架添加约束
        circleIcon.snp_makeConstraints { (make) in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80)
        }
        
        largeHouse.snp_makeConstraints { (make) in
            //            make.centerY.equalTo(circleIcon.snp_centerY)
            //            make.centerX.equalTo(circleIcon.snp_centerX)
            make.center.equalTo(circleIcon.snp_center)
        }
        
        //提示文案
        tipLabel.snp_makeConstraints { (make) in
            make.top.equalTo(circleIcon.snp_bottom).offset(16)
            make.centerX.equalTo(circleIcon.snp_centerX)
            //约束宽度
            //            make.width.equalTo(230)
            //约束高度 解决页面跳动的问题
            make.height.equalTo(40)
        }
        
        loginBtn.snp_makeConstraints { (make) in
            make.left.equalTo(tipLabel.snp_left)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
            
        }
        
        registerBtn.snp_makeConstraints { (make) in
            make.right.equalTo(tipLabel.snp_right)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        
        backIcon.snp_makeConstraints { (make) in
            make.center.equalTo(circleIcon.snp_center)
        }
        
        //一般使用sRGB取值
        let value: CGFloat = 237.0 / 255.0
        backgroundColor = UIColor(red: value, green: value, blue: value, alpha: 1)
        
        
        //添加按钮的点击事件
        loginBtn.addTarget(self, action: #selector(LS_VisitorLoginView.loginBtnDidClick), forControlEvents: .TouchUpInside)
        registerBtn.addTarget(self, action: #selector(LS_VisitorLoginView.registerBtnDidClick), forControlEvents: .TouchUpInside)
        
            }
    
    //MARK:添加按钮的点击事件
    @objc private func loginBtnDidClick(){
        visitorDelegate?.userWillLogin?()
    }
    @objc private func registerBtnDidClick(){
    visitorDelegate?.userWiilRegister()
    }

    //懒加载子视图
    //遮罩图片
    private lazy var backIcon: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //圈圈
    private lazy var circleIcon: UIImageView = UIImageView(image:UIImage(named: "visitordiscover_feed_image_smallicon"))
    //大房子
    private lazy var largeHouse:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //提示登录注册的 '文案'
    private lazy var tipLabel:UILabel = {
        
        let l = UILabel()
        l.text = "关注一些人,回到这里看看有什么惊喜"
        //字号 和颜色
        l.textColor = UIColor.darkGrayColor()
        l.font = UIFont.systemFontOfSize(14)
        l.textAlignment = .Center
        //设置自动换行
        l.numberOfLines = 0
        //最大的布局宽度
        l.preferredMaxLayoutWidth = 230
        //自适应大小
        l.sizeToFit()

        return l
    }()
    
    //登录按钮
    private lazy var loginBtn:UIButton = {
    
        let btn = UIButton()
        btn.setTitle("登录", forState: .Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        //设置文字大小
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        let image = UIImage(named: "common_button_white_disable")!
        let capW = Int(image.size.width * 0.5)
        let capH = Int(image.size.height * 0.5)
        btn.setBackgroundImage(image.stretchableImageWithLeftCapWidth(capW, topCapHeight: capH), forState: .Normal)
        
        return btn

        
    }()
    
    //注册按钮
    private lazy var registerBtn:UIButton = {
        
        let btn = UIButton()
        
        btn.setTitle("注册", forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        //设置文字大小
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        
        return btn

    
    }()
    
    
}
